﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.Lib;

namespace QuanLyBanHang.GUI
{
    public partial class frmKhoSanPham : Form
    {
        private DataTable dataTable;
        private int flag;
        public frmKhoSanPham()
        {
            
            InitializeComponent();
            disableControler();
            loadDataTable();
        }
        private void disableControler() {
            txtMaKho.Enabled = false;
            txtDiaChi.Enabled = false;
            txtGhiChu.Enabled = false;
            txtTenKho.Enabled = false;
            btnThemMoi.Enabled = true;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void enableControler() {
            txtMaKho.Enabled = true;
            txtDiaChi.Enabled = true;
            txtGhiChu.Enabled = true;
            txtTenKho.Enabled = true;
            btnThemMoi.Enabled = false;

        
        }
        private void textChange(){
            txtDiaChi.Text = "";
            txtGhiChu.Text = "";
            txtMaKho.Text = "";
            txtTenKho.Text = "";

        }
        private void loadDataTable() {
            dataTable = KetNoi.GetTable("select * from tblKho");
            dtgKho.DataSource = dataTable;
            dtgKho.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataTable = null;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            flag = 1;
            enableControler();
            textChange();
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int kq=0;
            if (flag == 1)
            {
                kq = KetNoi.ExcuteSQL("insert into tblKho values('" + txtMaKho.Text + "',N'" + txtTenKho.Text + "',N'" + txtDiaChi.Text + "',N'" + txtGhiChu.Text + "')");
                if (kq > 0)
                {

                    if (MessageBox.Show("Thông tin đã được lưu. Bạn có muốn nhập thêm kho ?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        disableControler();
                    }

                    loadDataTable();
                    textChange();
                        kq = 0;
                   // flag = 0;
                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }
            else {
                
                kq = KetNoi.ExcuteSQL("update tblKho set TenKho=N'" + txtTenKho.Text + "',DiaChi=N'" + txtDiaChi.Text + "',GhiChu=N'" + txtGhiChu.Text + "' where MaKho = '" + txtMaKho.Text + "'");
                if (kq > 0)
                {

                    MessageBox.Show("Thông tin đã được sửa");
                    loadDataTable();
                    textChange();
                    disableControler();
                    kq = 0;
                }
                else
                {
                    MessageBox.Show("Lỗi sảy ra.");
                }
            }
            
                
        }

        private void button4_Click(object sender, EventArgs e)
        {
            flag = 2;
            enableControler();
            txtMaKho.Enabled = false;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
           // btnXoa.Enabled = false;
        }

        private void dtgKho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataGridViewRow gridrow = dtgKho.CurrentRow;
                txtMaKho.Text = gridrow.Cells["MaKho"].Value.ToString();
                txtTenKho.Text = gridrow.Cells["TenKho"].Value.ToString();
                txtDiaChi.Text = gridrow.Cells["DiaChi"].Value.ToString();
                txtGhiChu.Text = gridrow.Cells["GhiChu"].Value.ToString();
                disableControler();
                txtMaKho.Enabled = false;
                btnLuu.Enabled = false;
                btnThemMoi.Enabled = true;
                btnXoa.Enabled = true;
                btnSua.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int kq;
            kq = KetNoi.ExcuteSQL("delete from tblKho where MaKho = '"+txtMaKho.Text+"'");
            if (kq > 0)
            {

                MessageBox.Show("Thông tin đã được sửa");


                loadDataTable();
                textChange();
                disableControler();
            }
            else
            {
                MessageBox.Show("Lỗi sảy ra.");
            }

        }

        private void txtloc_TextChanged(object sender, EventArgs e)
        {
            if (txtloc.Text != null) {
                dataTable = KetNoi.GetTable("select * from tblKho where MaKho like '%" + txtloc.Text + "%'");
                dtgKho.DataSource = dataTable;
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
